package com.hctforgreen.greenservice.ui.adapter;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.hctforgreen.greenservice.R;
import com.hctforgreen.greenservice.model.BaseEntity;
import com.hctforgreen.greenservice.model.NewsEntity;
import com.hctforgreen.greenservice.ui.handler.NewsListHandler;
import com.hctforgreen.greenservice.ui.page.AbstractDataLoaderHandler;
import com.hctforgreen.greenservice.ui.page.AbstractPageableAdapter;
import com.hctforgreen.greenservice.ui.widget.PullToRefreshListView;
import com.hctforgreen.greenservice.ui.widget.PullToRefreshListView.OnRefreshListener;

/**
 * 新闻适配器
 * 
 * @author dhb
 * @TIME
 */
public class NewsListAdapter extends AbstractPageableAdapter<BaseEntity>
		implements OnItemClickListener, OnRefreshListener {

	private Activity mActivity;
	private View mConvertView;
	private PullToRefreshListView mListView;
	private NewsListHandler mHandler;

	public NewsListAdapter(PullToRefreshListView listView, Activity activity,
			View convertView, int loadingViewResourceId,
			int reloadViewResourceId,
			AbstractDataLoaderHandler<BaseEntity> handler) {
		super(listView, activity, loadingViewResourceId, reloadViewResourceId,
				handler);
		mListView = listView;
		mActivity = activity;
		mConvertView = convertView;

		mHandler = (NewsListHandler) handler;
		mListView.setAdapter(this);
		mListView.setOnItemClickListener(this);
		mListView.setOnScrollListener(this);

		mListView.setOnRefreshListener(this);
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub

		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mActivity.getLayoutInflater().inflate(
					R.layout.item_news_list, null);
			holder.pic = (ImageView) convertView.findViewById(R.id.pic);
			holder.title_txt = (TextView) convertView
					.findViewById(R.id.title_txt);
			holder.time_txt = (TextView) convertView
					.findViewById(R.id.time_txt);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		NewsEntity entity = (NewsEntity) getItem(position);
		
		
		
		return convertView;
	}

	class ViewHolder {
		ImageView pic;
		TextView title_txt;
		TextView time_txt;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view,
			final int position, long id) {

	}

	@Override
	public void onRefresh() {
		mHandler.refreshTop(System.currentTimeMillis());
	}

}
